package se.study.socket;

import jdk.net.Sockets;

import java.io.*;
import java.net.MalformedURLException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;

/**
 * @Author: tiger
 * @Date:2018/6/29
 * @Description: 复习socket网络编程
 */
public class SocketServer {
    public static void main(String[] args) throws IOException {
        // 1.创建一个服务器端Socket，即ServerSocket，指定绑定的端口，并监听此端口
        ServerSocket serverSocket = new ServerSocket(6060);
        // 2.调用accept()方法开始监听，等待客户端的连接
        Socket accept = serverSocket.accept();
        // 3.获取输入流，并读取客户端信息
        InputStream inputStream = accept.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        // 4.读取信息
        String data = bufferedReader.readLine();
        while (data != null) {
            System.out.println(data);
            data = bufferedReader.readLine();
        }
        accept.shutdownInput();
        // 6.响应客户端
        OutputStream outputStream = accept.getOutputStream();
        PrintWriter printWriter = new PrintWriter(outputStream);
        printWriter.write("欢迎~~~~");
        printWriter.flush();
        accept.shutdownOutput();
        // 5.关闭资源
        printWriter.close();
        outputStream.close();
        bufferedReader.close();
        inputStreamReader.close();
        inputStream.close();
    }
}

